package com.anjufang.service.system.impl;

import com.anjufang.entity.AdminRoleMenu;
import com.anjufang.mapper.AdminRoleMenuMapper;
import com.anjufang.service.system.AdminRoleMenuService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminRoleMenuServiceImpl implements AdminRoleMenuService {
	
	@Autowired
	private AdminRoleMenuMapper adminRoleMenuMapper;
	
	@Override
	@Transactional
	public void saveOrUpdate(Long id, List<Long> menuIdList) {
		//先删除角色与菜单关系
		adminRoleMenuMapper.delete(new EntityWrapper<AdminRoleMenu>().where("role_id = {0}", id));
		
		if(menuIdList == null ||menuIdList.size() == 0){
			return ;
		}
		
		//保存角色与菜单关系
		Map<String, Object> map = new HashMap<>();
		map.put("roleId", id);
		map.put("permissions", menuIdList);
		adminRoleMenuMapper.setPermissions(map);
		
	}

}
